package com.starblink.imgsearch.result.ui.cell

import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.view.setPadding
import androidx.core.view.updateLayoutParams
import com.bumptech.glide.Glide
import com.starblink.android.basic.adapter.BaseVH
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.extension.dp2px
import com.starblink.imgsearch.R
import com.starblink.imgsearch.result.data.ImgClipModel

class ImgClipTabVH(parent: ViewGroup, private val onCellClick: ((position: Int) -> Unit)?) :
    BaseVH<ImgClipModel>(FrameLayout(parent.context).apply {
        layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }) {

    private val bgView = View(parent.context).apply {
        //setBackgroundResource(R.drawable.sp_img_clip_bg)
        setBackgroundResource(com.starblink.basic.style.R.drawable.ic_svg_bg_identify_clip)
    }

    private val imgView: ImageView = ImageView(parent.context)

    init {
        (itemView as FrameLayout).run {
            addView(bgView, FrameLayout.LayoutParams(
                36.dp2px(),
                36.dp2px()
            ).apply {
                gravity = Gravity.CENTER
            })
            addView(imgView, FrameLayout.LayoutParams(
                32.dp2px(),
                32.dp2px()
            ).apply {
                gravity = Gravity.CENTER
            })
        }
    }

    override fun onBind(model: ImgClipModel, position: Int) {
        bgView.visibility = if (model.selected) View.VISIBLE else View.GONE

        imgView.enableCorner(radius = 8.dp2px())
        imgView.scaleType = ImageView.ScaleType.CENTER_INSIDE
        imgView.setImageBitmap(model.imgBmp)
        imgView.setOnClickListener {
            onCellClick?.invoke(position)
        }
        imgView
    }
}